<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>range_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_dom_range_test.js.html">range_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.dom.RangeTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.dom.RangeTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.dom&#39;);
<a name="line19"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line20"></a>goog.require(&#39;goog.dom.Range&#39;);
<a name="line21"></a>goog.require(&#39;goog.dom.RangeType&#39;);
<a name="line22"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line23"></a>goog.require(&#39;goog.dom.TextRange&#39;);
<a name="line24"></a>goog.require(&#39;goog.dom.browserrange&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.dom&#39;);
<a name="line26"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line27"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line28"></a>
<a name="line29"></a>var assertRangeEquals = goog.testing.dom.assertRangeEquals;
<a name="line30"></a>
<a name="line31"></a>function setUp() {
<a name="line32"></a>  // Reset the focus; some tests may invalidate the focus to exercise various
<a name="line33"></a>  // browser bugs.
<a name="line34"></a>  var focusableElement = goog.dom.getElement(&#39;focusableElement&#39;);
<a name="line35"></a>  focusableElement.focus();
<a name="line36"></a>  focusableElement.blur();
<a name="line37"></a>}
<a name="line38"></a>
<a name="line39"></a>function normalizeHtml(str) {
<a name="line40"></a>  return str.toLowerCase().replace(/[\n\r\f&quot;]/g, &#39;&#39;)
<a name="line41"></a>      .replace(/&lt;\/li&gt;/g, &#39;&#39;); // &quot; for emacs
<a name="line42"></a>}
<a name="line43"></a>
<a name="line44"></a>function testCreate() {
<a name="line45"></a>  assertNotNull(&#39;Browser range object can be created for node&#39;,
<a name="line46"></a>      goog.dom.Range.createFromNodeContents(goog.dom.getElement(&#39;test1&#39;)));
<a name="line47"></a>}
<a name="line48"></a>
<a name="line49"></a>function testTableRange() {
<a name="line50"></a>  var tr = goog.dom.getElement(&#39;cell&#39;).parentNode;
<a name="line51"></a>  var range = goog.dom.Range.createFromNodeContents(tr);
<a name="line52"></a>  assertEquals(&#39;Selection should have correct text&#39;, &#39;12&#39;,
<a name="line53"></a>      range.getText());
<a name="line54"></a>  assertEquals(&#39;Selection should have correct html fragment&#39;,
<a name="line55"></a>      &#39;1&lt;/td&gt;&lt;td&gt;2&#39;, normalizeHtml(range.getHtmlFragment()));
<a name="line56"></a>
<a name="line57"></a>  // TODO(robbyw): On IE the TR is included, on FF it is not.
<a name="line58"></a>  //assertEquals(&#39;Selection should have correct valid html&#39;,
<a name="line59"></a>  //    &#39;&lt;tr id=row&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&#39;,
<a name="line60"></a>  //    normalizeHtml(range.getValidHtml()));
<a name="line61"></a>
<a name="line62"></a>  assertEquals(&#39;Selection should have correct pastable html&#39;,
<a name="line63"></a>      &#39;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td id=cell&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&#39;,
<a name="line64"></a>      normalizeHtml(range.getPastableHtml()));
<a name="line65"></a>}
<a name="line66"></a>
<a name="line67"></a>function testUnorderedListRange() {
<a name="line68"></a>  var ul = goog.dom.getElement(&#39;ulTest&#39;).firstChild;
<a name="line69"></a>  var range = goog.dom.Range.createFromNodeContents(ul);
<a name="line70"></a>  assertEquals(&#39;Selection should have correct html fragment&#39;,
<a name="line71"></a>      &#39;1&lt;li&gt;2&#39;, normalizeHtml(range.getHtmlFragment()));
<a name="line72"></a>
<a name="line73"></a>  // TODO(robbyw): On IE the UL is included, on FF it is not.
<a name="line74"></a>  //assertEquals(&#39;Selection should have correct valid html&#39;,
<a name="line75"></a>  //    &#39;&lt;li&gt;1&lt;/li&gt;&lt;li&gt;2&lt;/li&gt;&#39;, normalizeHtml(range.getValidHtml()));
<a name="line76"></a>
<a name="line77"></a>  assertEquals(&#39;Selection should have correct pastable html&#39;,
<a name="line78"></a>      &#39;&lt;ul&gt;&lt;li&gt;1&lt;li&gt;2&lt;/ul&gt;&#39;,
<a name="line79"></a>      normalizeHtml(range.getPastableHtml()));
<a name="line80"></a>}
<a name="line81"></a>
<a name="line82"></a>function testOrderedListRange() {
<a name="line83"></a>  var ol = goog.dom.getElement(&#39;olTest&#39;).firstChild;
<a name="line84"></a>  var range = goog.dom.Range.createFromNodeContents(ol);
<a name="line85"></a>  assertEquals(&#39;Selection should have correct html fragment&#39;,
<a name="line86"></a>      &#39;1&lt;li&gt;2&#39;, normalizeHtml(range.getHtmlFragment()));
<a name="line87"></a>
<a name="line88"></a>  // TODO(robbyw): On IE the OL is included, on FF it is not.
<a name="line89"></a>  //assertEquals(&#39;Selection should have correct valid html&#39;,
<a name="line90"></a>  //    &#39;&lt;li&gt;1&lt;/li&gt;&lt;li&gt;2&lt;/li&gt;&#39;, normalizeHtml(range.getValidHtml()));
<a name="line91"></a>
<a name="line92"></a>  assertEquals(&#39;Selection should have correct pastable html&#39;,
<a name="line93"></a>      &#39;&lt;ol&gt;&lt;li&gt;1&lt;li&gt;2&lt;/ol&gt;&#39;,
<a name="line94"></a>      normalizeHtml(range.getPastableHtml()));
<a name="line95"></a>}
<a name="line96"></a>
<a name="line97"></a>function testCreateFromNodes() {
<a name="line98"></a>  var start = goog.dom.getElement(&#39;test1&#39;).firstChild;
<a name="line99"></a>  var end = goog.dom.getElement(&#39;br&#39;);
<a name="line100"></a>  var range = goog.dom.Range.createFromNodes(start, 2, end, 0);
<a name="line101"></a>  assertNotNull(&#39;Browser range object can be created for W3C node range&#39;,
<a name="line102"></a>      range);
<a name="line103"></a>
<a name="line104"></a>  assertEquals(&#39;Start node should be selected at start endpoint&#39;, start,
<a name="line105"></a>      range.getStartNode());
<a name="line106"></a>  assertEquals(&#39;Selection should start at offset 2&#39;, 2,
<a name="line107"></a>      range.getStartOffset());
<a name="line108"></a>  assertEquals(&#39;Start node should be selected at anchor endpoint&#39;, start,
<a name="line109"></a>      range.getAnchorNode());
<a name="line110"></a>  assertEquals(&#39;Selection should be anchored at offset 2&#39;, 2,
<a name="line111"></a>      range.getAnchorOffset());
<a name="line112"></a>
<a name="line113"></a>  var div = goog.dom.getElement(&#39;test2&#39;);
<a name="line114"></a>  assertEquals(&#39;DIV node should be selected at end endpoint&#39;, div,
<a name="line115"></a>      range.getEndNode());
<a name="line116"></a>  assertEquals(&#39;Selection should end at offset 1&#39;, 1, range.getEndOffset());
<a name="line117"></a>  assertEquals(&#39;DIV node should be selected at focus endpoint&#39;, div,
<a name="line118"></a>      range.getFocusNode());
<a name="line119"></a>  assertEquals(&#39;Selection should be focused at offset 1&#39;, 1,
<a name="line120"></a>      range.getFocusOffset());
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>  assertTrue(&#39;Text content should be &quot;xt\\s*abc&quot;&#39;,
<a name="line124"></a>      /xt\s*abc/.test(range.getText()));
<a name="line125"></a>  assertFalse(&#39;Nodes range is not collapsed&#39;, range.isCollapsed());
<a name="line126"></a>}
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>function testCreateControlRange() {
<a name="line130"></a>  if (!goog.userAgent.IE) {
<a name="line131"></a>    return;
<a name="line132"></a>  }
<a name="line133"></a>  var cr = document.body.createControlRange();
<a name="line134"></a>  cr.addElement(goog.dom.getElement(&#39;logo&#39;));
<a name="line135"></a>
<a name="line136"></a>  var range = goog.dom.Range.createFromBrowserRange(cr);
<a name="line137"></a>  assertNotNull(&#39;Control range object can be created from browser range&#39;,
<a name="line138"></a>      range);
<a name="line139"></a>  assertEquals(&#39;Created range is a control range&#39;, goog.dom.RangeType.CONTROL,
<a name="line140"></a>      range.getType());
<a name="line141"></a>}
<a name="line142"></a>
<a name="line143"></a>
<a name="line144"></a>function testTextNode() {
<a name="line145"></a>  var range = goog.dom.Range.createFromNodeContents(
<a name="line146"></a>      goog.dom.getElement(&#39;test1&#39;).firstChild);
<a name="line147"></a>
<a name="line148"></a>  assertEquals(&#39;Created range is a text range&#39;, goog.dom.RangeType.TEXT,
<a name="line149"></a>      range.getType());
<a name="line150"></a>  assertEquals(&#39;Text node should be selected at start endpoint&#39;, &#39;Text&#39;,
<a name="line151"></a>      range.getStartNode().nodeValue);
<a name="line152"></a>  assertEquals(&#39;Selection should start at offset 0&#39;, 0,
<a name="line153"></a>      range.getStartOffset());
<a name="line154"></a>
<a name="line155"></a>  assertEquals(&#39;Text node should be selected at end endpoint&#39;, &#39;Text&#39;,
<a name="line156"></a>      range.getEndNode().nodeValue);
<a name="line157"></a>  assertEquals(&#39;Selection should end at offset 4&#39;, &#39;Text&#39;.length,
<a name="line158"></a>      range.getEndOffset());
<a name="line159"></a>
<a name="line160"></a>  assertEquals(&#39;Container should be text node&#39;, goog.dom.NodeType.TEXT,
<a name="line161"></a>      range.getContainer().nodeType);
<a name="line162"></a>
<a name="line163"></a>  assertEquals(&#39;Text content should be &quot;Text&quot;&#39;, &#39;Text&#39;, range.getText());
<a name="line164"></a>  assertFalse(&#39;Text range is not collapsed&#39;, range.isCollapsed());
<a name="line165"></a>}
<a name="line166"></a>
<a name="line167"></a>
<a name="line168"></a>function testDiv() {
<a name="line169"></a>  var range = goog.dom.Range.createFromNodeContents(
<a name="line170"></a>      goog.dom.getElement(&#39;test2&#39;));
<a name="line171"></a>
<a name="line172"></a>  assertEquals(&#39;Text node &quot;abc&quot; should be selected at start endpoint&#39;, &#39;abc&#39;,
<a name="line173"></a>      range.getStartNode().nodeValue);
<a name="line174"></a>  assertEquals(&#39;Selection should start at offset 0&#39;, 0,
<a name="line175"></a>      range.getStartOffset());
<a name="line176"></a>
<a name="line177"></a>  assertEquals(&#39;Text node &quot;def&quot; should be selected at end endpoint&#39;, &#39;def&#39;,
<a name="line178"></a>      range.getEndNode().nodeValue);
<a name="line179"></a>  assertEquals(&#39;Selection should end at offset 3&#39;, &#39;def&#39;.length,
<a name="line180"></a>      range.getEndOffset());
<a name="line181"></a>
<a name="line182"></a>  assertEquals(&#39;Container should be DIV&#39;, goog.dom.getElement(&#39;test2&#39;),
<a name="line183"></a>      range.getContainer());
<a name="line184"></a>
<a name="line185"></a>  assertTrue(&#39;Div text content should be &quot;abc\\s*def&quot;&#39;,
<a name="line186"></a>      /abc\s*def/.test(range.getText()));
<a name="line187"></a>  assertFalse(&#39;Div range is not collapsed&#39;, range.isCollapsed());
<a name="line188"></a>}
<a name="line189"></a>
<a name="line190"></a>
<a name="line191"></a>function testEmptyNode() {
<a name="line192"></a>  var range = goog.dom.Range.createFromNodeContents(
<a name="line193"></a>      goog.dom.getElement(&#39;empty&#39;));
<a name="line194"></a>
<a name="line195"></a>  assertEquals(&#39;DIV be selected at start endpoint&#39;,
<a name="line196"></a>      goog.dom.getElement(&#39;empty&#39;), range.getStartNode());
<a name="line197"></a>  assertEquals(&#39;Selection should start at offset 0&#39;, 0,
<a name="line198"></a>      range.getStartOffset());
<a name="line199"></a>
<a name="line200"></a>  assertEquals(&#39;DIV should be selected at end endpoint&#39;,
<a name="line201"></a>      goog.dom.getElement(&#39;empty&#39;), range.getEndNode());
<a name="line202"></a>  assertEquals(&#39;Selection should end at offset 0&#39;, 0,
<a name="line203"></a>      range.getEndOffset());
<a name="line204"></a>
<a name="line205"></a>  assertEquals(&#39;Container should be DIV&#39;, goog.dom.getElement(&#39;empty&#39;),
<a name="line206"></a>      range.getContainer());
<a name="line207"></a>
<a name="line208"></a>  assertEquals(&#39;Empty text content should be &quot;&quot;&#39;, &#39;&#39;, range.getText());
<a name="line209"></a>  assertTrue(&#39;Empty range is collapsed&#39;, range.isCollapsed());
<a name="line210"></a>}
<a name="line211"></a>
<a name="line212"></a>
<a name="line213"></a>function testCollapse() {
<a name="line214"></a>  var range = goog.dom.Range.createFromNodeContents(
<a name="line215"></a>      goog.dom.getElement(&#39;test2&#39;));
<a name="line216"></a>  assertFalse(&#39;Div range is not collapsed&#39;, range.isCollapsed());
<a name="line217"></a>  range.collapse();
<a name="line218"></a>  assertTrue(&#39;Div range is collapsed after call to empty()&#39;,
<a name="line219"></a>      range.isCollapsed());
<a name="line220"></a>
<a name="line221"></a>  range = goog.dom.Range.createFromNodeContents(goog.dom.getElement(&#39;empty&#39;));
<a name="line222"></a>  assertTrue(&#39;Empty range is collapsed&#39;, range.isCollapsed());
<a name="line223"></a>  range.collapse();
<a name="line224"></a>  assertTrue(&#39;Empty range is still collapsed&#39;, range.isCollapsed());
<a name="line225"></a>}
<a name="line226"></a>
<a name="line227"></a>// TODO(robbyw): Test iteration over a strange document fragment.
<a name="line228"></a>
<a name="line229"></a>function testIterator() {
<a name="line230"></a>  goog.testing.dom.assertNodesMatch(goog.dom.Range.createFromNodeContents(
<a name="line231"></a>      goog.dom.getElement(&#39;test2&#39;)), [&#39;abc&#39;, &#39;#br&#39;, &#39;#br&#39;, &#39;def&#39;]);
<a name="line232"></a>}
<a name="line233"></a>
<a name="line234"></a>function testReversedNodes() {
<a name="line235"></a>  var node = goog.dom.getElement(&#39;test1&#39;).firstChild;
<a name="line236"></a>  var range = goog.dom.Range.createFromNodes(node, 4, node, 0);
<a name="line237"></a>  assertTrue(&#39;Range is reversed&#39;, range.isReversed());
<a name="line238"></a>  node = goog.dom.getElement(&#39;test3&#39;);
<a name="line239"></a>  range = goog.dom.Range.createFromNodes(node, 0, node, 1);
<a name="line240"></a>  assertFalse(&#39;Range is not reversed&#39;, range.isReversed());
<a name="line241"></a>}
<a name="line242"></a>
<a name="line243"></a>function testReversedContents() {
<a name="line244"></a>  var range = goog.dom.Range.createFromNodeContents(
<a name="line245"></a>      goog.dom.getElement(&#39;test1&#39;), true);
<a name="line246"></a>  assertTrue(&#39;Range is reversed&#39;, range.isReversed());
<a name="line247"></a>  assertEquals(&#39;Range should select &quot;Text&quot;&#39;, &#39;Text&#39;,
<a name="line248"></a>      range.getText());
<a name="line249"></a>  assertEquals(&#39;Range start offset should be 0&#39;, 0, range.getStartOffset());
<a name="line250"></a>  assertEquals(&#39;Range end offset should be 4&#39;, 4, range.getEndOffset());
<a name="line251"></a>  assertEquals(&#39;Range anchor offset should be 4&#39;, 4, range.getAnchorOffset());
<a name="line252"></a>  assertEquals(&#39;Range focus offset should be 0&#39;, 0, range.getFocusOffset());
<a name="line253"></a>
<a name="line254"></a>  var range2 = range.clone();
<a name="line255"></a>
<a name="line256"></a>  range.collapse(true);
<a name="line257"></a>  assertTrue(&#39;Range is collapsed&#39;, range.isCollapsed());
<a name="line258"></a>  assertFalse(&#39;Collapsed range is not reversed&#39;, range.isReversed());
<a name="line259"></a>  assertEquals(&#39;Post collapse start offset should be 4&#39;, 4,
<a name="line260"></a>      range.getStartOffset());
<a name="line261"></a>
<a name="line262"></a>  range2.collapse(false);
<a name="line263"></a>  assertTrue(&#39;Range 2 is collapsed&#39;, range2.isCollapsed());
<a name="line264"></a>  assertFalse(&#39;Collapsed range 2 is not reversed&#39;, range2.isReversed());
<a name="line265"></a>  assertEquals(&#39;Post collapse start offset 2 should be 0&#39;, 0,
<a name="line266"></a>      range2.getStartOffset());
<a name="line267"></a>}
<a name="line268"></a>
<a name="line269"></a>function testRemoveContents() {
<a name="line270"></a>  var outer = goog.dom.getElement(&#39;removeTest&#39;);
<a name="line271"></a>  var range = goog.dom.Range.createFromNodeContents(outer.firstChild);
<a name="line272"></a>
<a name="line273"></a>  range.removeContents();
<a name="line274"></a>
<a name="line275"></a>  assertEquals(&#39;Removed range content should be &quot;&quot;&#39;, &#39;&#39;, range.getText());
<a name="line276"></a>  assertTrue(&#39;Removed range should be collapsed&#39;, range.isCollapsed());
<a name="line277"></a>  assertEquals(&#39;Outer div should have 1 child now&#39;, 1,
<a name="line278"></a>      outer.childNodes.length);
<a name="line279"></a>  assertEquals(&#39;Inner div should be empty&#39;, 0,
<a name="line280"></a>      outer.firstChild.childNodes.length);
<a name="line281"></a>}
<a name="line282"></a>
<a name="line283"></a>function testRemovePartialContents() {
<a name="line284"></a>  var outer = goog.dom.getElement(&#39;removePartialTest&#39;);
<a name="line285"></a>
<a name="line286"></a>  var range = goog.dom.Range.createFromNodes(outer.firstChild, 2,
<a name="line287"></a>      outer.firstChild, 4);
<a name="line288"></a>  removeHelper(1, range, outer, 1, &#39;0145&#39;);
<a name="line289"></a>
<a name="line290"></a>  range = goog.dom.Range.createFromNodes(outer.firstChild, 0,
<a name="line291"></a>      outer.firstChild, 1);
<a name="line292"></a>  removeHelper(2, range, outer, 1, &#39;145&#39;);
<a name="line293"></a>
<a name="line294"></a>  range = goog.dom.Range.createFromNodes(outer.firstChild, 2,
<a name="line295"></a>      outer.firstChild, 3);
<a name="line296"></a>  removeHelper(3, range, outer, 1, &#39;14&#39;);
<a name="line297"></a>
<a name="line298"></a>  var br = goog.dom.createDom(&#39;BR&#39;);
<a name="line299"></a>  outer.appendChild(br);
<a name="line300"></a>  range = goog.dom.Range.createFromNodes(outer.firstChild, 1,
<a name="line301"></a>      outer, 1);
<a name="line302"></a>  removeHelper(4, range, outer, 2, &#39;1&lt;br&gt;&#39;);
<a name="line303"></a>
<a name="line304"></a>  outer.innerHTML = &#39;&lt;br&gt;123&#39;;
<a name="line305"></a>  range = goog.dom.Range.createFromNodes(outer, 0, outer.lastChild, 2);
<a name="line306"></a>  removeHelper(5, range, outer, 1, &#39;3&#39;);
<a name="line307"></a>
<a name="line308"></a>  outer.innerHTML = &#39;123&lt;br&gt;456&#39;;
<a name="line309"></a>  range = goog.dom.Range.createFromNodes(outer.firstChild, 1, outer.lastChild,
<a name="line310"></a>      2);
<a name="line311"></a>  removeHelper(6, range, outer, 2, &#39;16&#39;);
<a name="line312"></a>
<a name="line313"></a>  outer.innerHTML = &#39;123&lt;br&gt;456&#39;;
<a name="line314"></a>  range = goog.dom.Range.createFromNodes(outer.firstChild, 0, outer.lastChild,
<a name="line315"></a>      2);
<a name="line316"></a>  removeHelper(7, range, outer, 1, &#39;6&#39;);
<a name="line317"></a>
<a name="line318"></a>  outer.innerHTML = &#39;&lt;div&gt;&lt;/div&gt;&#39;;
<a name="line319"></a>  range = goog.dom.Range.createFromNodeContents(outer.firstChild);
<a name="line320"></a>  removeHelper(8, range, outer, 1, &#39;&lt;div&gt;&lt;/div&gt;&#39;);
<a name="line321"></a>
<a name="line322"></a>  // TODO(robbyw): Fix the following edge cases:
<a name="line323"></a>  //    * Selecting contents of a node containing multiply empty divs
<a name="line324"></a>  //    * Selecting via createFromNodes(x, 0, x, x.childNodes.length)
<a name="line325"></a>  //    * Consistent handling of nodeContents(&lt;div&gt;&lt;div&gt;&lt;/div&gt;&lt;/div&gt;).remove
<a name="line326"></a>}
<a name="line327"></a>
<a name="line328"></a>function removeHelper(testNumber, range, outer, expectedChildCount,
<a name="line329"></a>    expectedContent) {
<a name="line330"></a>  range.removeContents();
<a name="line331"></a>  assertTrue(testNumber + &#39;: Removed range should now be collapsed&#39;,
<a name="line332"></a>      range.isCollapsed());
<a name="line333"></a>  assertEquals(testNumber + &#39;: Removed range content should be &quot;&quot;&#39;, &#39;&#39;,
<a name="line334"></a>      range.getText());
<a name="line335"></a>  assertEquals(testNumber + &#39;: Outer div should contain correct text&#39;,
<a name="line336"></a>      expectedContent, outer.innerHTML.toLowerCase());
<a name="line337"></a>  assertEquals(testNumber + &#39;: Outer div should have &#39; + expectedChildCount +
<a name="line338"></a>      &#39; children now&#39;, expectedChildCount, outer.childNodes.length);
<a name="line339"></a>  assertNotNull(testNumber + &#39;: Empty node should still exist&#39;,
<a name="line340"></a>      goog.dom.getElement(&#39;empty&#39;));
<a name="line341"></a>}
<a name="line342"></a>
<a name="line343"></a>function testSurroundContents() {
<a name="line344"></a>  var outer = goog.dom.getElement(&#39;surroundTest&#39;);
<a name="line345"></a>  outer.innerHTML = &#39;---Text that&lt;br&gt;will be surrounded---&#39;;
<a name="line346"></a>  var range = goog.dom.Range.createFromNodes(outer.firstChild, 3,
<a name="line347"></a>      outer.lastChild, outer.lastChild.nodeValue.length - 3);
<a name="line348"></a>
<a name="line349"></a>  var div = goog.dom.createDom(goog.dom.TagName.DIV, {&#39;style&#39;: &#39;color: red&#39;});
<a name="line350"></a>  var output = range.surroundContents(div);
<a name="line351"></a>
<a name="line352"></a>  assertEquals(&#39;Outer element should contain new element&#39;, outer,
<a name="line353"></a>      output.parentNode);
<a name="line354"></a>  assertFalse(&#39;New element should have no id&#39;, !!output.id);
<a name="line355"></a>  assertEquals(&#39;New element should be red&#39;, &#39;red&#39;, output.style.color);
<a name="line356"></a>  assertEquals(&#39;Outer element should have three children&#39;, 3,
<a name="line357"></a>      outer.childNodes.length);
<a name="line358"></a>  assertEquals(&#39;New element should have three children&#39;, 3,
<a name="line359"></a>      output.childNodes.length);
<a name="line360"></a>
<a name="line361"></a>  // TODO(robbyw): Ensure the range stays in a reasonable state.
<a name="line362"></a>}
<a name="line363"></a>
<a name="line364"></a>
<a name="line365"></a>/**
<a name="line366"></a> * Given two offsets into the &#39;foobar&#39; node, make sure that inserting
<a name="line367"></a> * nodes at those offsets doesn&#39;t change a selection of &#39;oba&#39;.
<a name="line368"></a> * @bug 1480638
<a name="line369"></a> */
<a name="line370"></a>function assertSurroundDoesntChangeSelectionWithOffsets(
<a name="line371"></a>    offset1, offset2, expectedHtml) {
<a name="line372"></a>  var div = goog.dom.getElement(&#39;bug1480638&#39;);
<a name="line373"></a>  div.innerHTML = &#39;foobar&#39;;
<a name="line374"></a>  var rangeToSelect = goog.dom.Range.createFromNodes(
<a name="line375"></a>      div.firstChild, 2, div.firstChild, 5);
<a name="line376"></a>  rangeToSelect.select();
<a name="line377"></a>
<a name="line378"></a>  var rangeToSurround = goog.dom.Range.createFromNodes(
<a name="line379"></a>      div.firstChild, offset1, div.firstChild, offset2);
<a name="line380"></a>  rangeToSurround.surroundWithNodes(goog.dom.createDom(&#39;span&#39;),
<a name="line381"></a>      goog.dom.createDom(&#39;span&#39;));
<a name="line382"></a>
<a name="line383"></a>  // Make sure that the selection didn&#39;t change.
<a name="line384"></a>  assertHTMLEquals(&#39;Selection must not change when contents are surrounded.&#39;,
<a name="line385"></a>      expectedHtml, goog.dom.Range.createFromWindow().getHtmlFragment());
<a name="line386"></a>}
<a name="line387"></a>
<a name="line388"></a>function testSurroundWithNodesDoesntChangeSelection1() {
<a name="line389"></a>  assertSurroundDoesntChangeSelectionWithOffsets(3, 4,
<a name="line390"></a>      &#39;o&lt;span&gt;&lt;/span&gt;b&lt;span&gt;&lt;/span&gt;a&#39;);
<a name="line391"></a>}
<a name="line392"></a>
<a name="line393"></a>function testSurroundWithNodesDoesntChangeSelection2() {
<a name="line394"></a>  assertSurroundDoesntChangeSelectionWithOffsets(3, 6,
<a name="line395"></a>      &#39;o&lt;span&gt;&lt;/span&gt;ba&#39;);
<a name="line396"></a>}
<a name="line397"></a>
<a name="line398"></a>function testSurroundWithNodesDoesntChangeSelection3() {
<a name="line399"></a>  assertSurroundDoesntChangeSelectionWithOffsets(1, 3,
<a name="line400"></a>      &#39;o&lt;span&gt;&lt;/span&gt;ba&#39;);
<a name="line401"></a>}
<a name="line402"></a>
<a name="line403"></a>function testSurroundWithNodesDoesntChangeSelection4() {
<a name="line404"></a>  assertSurroundDoesntChangeSelectionWithOffsets(1, 6,
<a name="line405"></a>      &#39;oba&#39;);
<a name="line406"></a>}
<a name="line407"></a>
<a name="line408"></a>function testInsertNode() {
<a name="line409"></a>  var outer = goog.dom.getElement(&#39;insertTest&#39;);
<a name="line410"></a>  outer.innerHTML = &#39;ACD&#39;;
<a name="line411"></a>
<a name="line412"></a>  var range = goog.dom.Range.createFromNodes(outer.firstChild, 1,
<a name="line413"></a>      outer.firstChild, 2);
<a name="line414"></a>  range.insertNode(goog.dom.createTextNode(&#39;B&#39;), true);
<a name="line415"></a>  assertEquals(&#39;Element should have correct innerHTML&#39;, &#39;ABCD&#39;,
<a name="line416"></a>      outer.innerHTML);
<a name="line417"></a>
<a name="line418"></a>  outer.innerHTML = &#39;12&#39;;
<a name="line419"></a>  range = goog.dom.Range.createFromNodes(outer.firstChild, 0,
<a name="line420"></a>      outer.firstChild, 1);
<a name="line421"></a>  var br = range.insertNode(goog.dom.createDom(goog.dom.TagName.BR), false);
<a name="line422"></a>  assertEquals(&#39;New element should have correct innerHTML&#39;, &#39;1&lt;br&gt;2&#39;,
<a name="line423"></a>      outer.innerHTML.toLowerCase());
<a name="line424"></a>  assertEquals(&#39;BR should be in outer&#39;, outer, br.parentNode);
<a name="line425"></a>}
<a name="line426"></a>
<a name="line427"></a>function testReplaceContentsWithNode() {
<a name="line428"></a>  var outer = goog.dom.getElement(&#39;insertTest&#39;);
<a name="line429"></a>  outer.innerHTML = &#39;AXC&#39;;
<a name="line430"></a>
<a name="line431"></a>  var range = goog.dom.Range.createFromNodes(outer.firstChild, 1,
<a name="line432"></a>      outer.firstChild, 2);
<a name="line433"></a>  range.replaceContentsWithNode(goog.dom.createTextNode(&#39;B&#39;));
<a name="line434"></a>  assertEquals(&#39;Element should have correct innerHTML&#39;, &#39;ABC&#39;,
<a name="line435"></a>      outer.innerHTML);
<a name="line436"></a>
<a name="line437"></a>  outer.innerHTML = &#39;ABC&#39;;
<a name="line438"></a>  range = goog.dom.Range.createFromNodes(outer.firstChild, 3,
<a name="line439"></a>      outer.firstChild, 3);
<a name="line440"></a>  range.replaceContentsWithNode(goog.dom.createTextNode(&#39;D&#39;));
<a name="line441"></a>  assertEquals(
<a name="line442"></a>      &#39;Element should have correct innerHTML after collapsed replace&#39;,
<a name="line443"></a>      &#39;ABCD&#39;, outer.innerHTML);
<a name="line444"></a>
<a name="line445"></a>  outer.innerHTML = &#39;AX&lt;b&gt;X&lt;/b&gt;XC&#39;;
<a name="line446"></a>  range = goog.dom.Range.createFromNodes(outer.firstChild, 1,
<a name="line447"></a>      outer.lastChild, 1);
<a name="line448"></a>  range.replaceContentsWithNode(goog.dom.createTextNode(&#39;B&#39;));
<a name="line449"></a>  goog.testing.dom.assertHtmlContentsMatch(&#39;ABC&#39;, outer);
<a name="line450"></a>}
<a name="line451"></a>
<a name="line452"></a>function testSurroundWithNodes() {
<a name="line453"></a>  var outer = goog.dom.getElement(&#39;insertTest&#39;);
<a name="line454"></a>  outer.innerHTML = &#39;ACE&#39;;
<a name="line455"></a>  var range = goog.dom.Range.createFromNodes(outer.firstChild, 1,
<a name="line456"></a>      outer.firstChild, 2);
<a name="line457"></a>
<a name="line458"></a>  range.surroundWithNodes(goog.dom.createTextNode(&#39;B&#39;),
<a name="line459"></a>      goog.dom.createTextNode(&#39;D&#39;));
<a name="line460"></a>
<a name="line461"></a>  assertEquals(&#39;New element should have correct innerHTML&#39;, &#39;ABCDE&#39;,
<a name="line462"></a>      outer.innerHTML);
<a name="line463"></a>}
<a name="line464"></a>
<a name="line465"></a>function testIsRangeInDocument() {
<a name="line466"></a>  var outer = goog.dom.getElement(&#39;insertTest&#39;);
<a name="line467"></a>  outer.innerHTML = &#39;&lt;br&gt;ABC&#39;;
<a name="line468"></a>  var range = goog.dom.Range.createCaret(outer.lastChild, 1);
<a name="line469"></a>
<a name="line470"></a>  assertEquals(&#39;Should get correct start element&#39;, &#39;ABC&#39;,
<a name="line471"></a>      range.getStartNode().nodeValue);
<a name="line472"></a>  assertTrue(&#39;Should be considered in document&#39;, range.isRangeInDocument());
<a name="line473"></a>
<a name="line474"></a>  outer.innerHTML = &#39;DEF&#39;;
<a name="line475"></a>
<a name="line476"></a>  assertFalse(&#39;Should be marked as out of document&#39;,
<a name="line477"></a>      range.isRangeInDocument());
<a name="line478"></a>}
<a name="line479"></a>
<a name="line480"></a>function testRemovedNode() {
<a name="line481"></a>  var node = goog.dom.getElement(&#39;removeNodeTest&#39;);
<a name="line482"></a>  var range = goog.dom.browserrange.createRangeFromNodeContents(node);
<a name="line483"></a>  range.select();
<a name="line484"></a>  goog.dom.removeNode(node);
<a name="line485"></a>
<a name="line486"></a>  var newRange = goog.dom.Range.createFromWindow(window);
<a name="line487"></a>
<a name="line488"></a>  // In Chrome 14 and below (&lt;= Webkit 535.1), newRange will be null.
<a name="line489"></a>  // In Chrome 16 and above (&gt;= Webkit 535.7), newRange will be collapsed
<a name="line490"></a>  // like on other browsers.
<a name="line491"></a>  // We didn&#39;t bother testing in between.
<a name="line492"></a>  if (goog.userAgent.WEBKIT &amp;&amp; !goog.userAgent.isVersionOrHigher(&#39;535.7&#39;)) {
<a name="line493"></a>    assertNull(&#39;Webkit supports rangeCount == 0&#39;, newRange);
<a name="line494"></a>  } else {
<a name="line495"></a>    assertTrue(&#39;The other browsers will just have an empty range.&#39;,
<a name="line496"></a>        newRange.isCollapsed());
<a name="line497"></a>  }
<a name="line498"></a>}
<a name="line499"></a>
<a name="line500"></a>function testReversedRange() {
<a name="line501"></a>  goog.dom.Range.createFromNodes(goog.dom.getElement(&#39;test2&#39;), 0,
<a name="line502"></a>      goog.dom.getElement(&#39;test1&#39;), 0).select();
<a name="line503"></a>
<a name="line504"></a>  var range = goog.dom.Range.createFromWindow(window);
<a name="line505"></a>  assertTrue(&#39;Range should be reversed&#39;,
<a name="line506"></a>      goog.userAgent.IE || range.isReversed());
<a name="line507"></a>}
<a name="line508"></a>
<a name="line509"></a>function testUnreversedRange() {
<a name="line510"></a>  goog.dom.Range.createFromNodes(goog.dom.getElement(&#39;test1&#39;), 0,
<a name="line511"></a>      goog.dom.getElement(&#39;test2&#39;), 0).select();
<a name="line512"></a>
<a name="line513"></a>  var range = goog.dom.Range.createFromWindow(window);
<a name="line514"></a>  assertFalse(&#39;Range should not be reversed&#39;, range.isReversed());
<a name="line515"></a>}
<a name="line516"></a>
<a name="line517"></a>function testReversedThenUnreversedRange() {
<a name="line518"></a>  // This tests a workaround for a webkit bug where webkit caches selections
<a name="line519"></a>  // incorrectly.
<a name="line520"></a>  goog.dom.Range.createFromNodes(goog.dom.getElement(&#39;test2&#39;), 0,
<a name="line521"></a>      goog.dom.getElement(&#39;test1&#39;), 0).select();
<a name="line522"></a>  goog.dom.Range.createFromNodes(goog.dom.getElement(&#39;test1&#39;), 0,
<a name="line523"></a>      goog.dom.getElement(&#39;test2&#39;), 0).select();
<a name="line524"></a>
<a name="line525"></a>  var range = goog.dom.Range.createFromWindow(window);
<a name="line526"></a>  assertFalse(&#39;Range should not be reversed&#39;, range.isReversed());
<a name="line527"></a>}
<a name="line528"></a>
<a name="line529"></a>function testHasAndClearSelection() {
<a name="line530"></a>  goog.dom.Range.createFromNodeContents(
<a name="line531"></a>      goog.dom.getElement(&#39;test1&#39;)).select();
<a name="line532"></a>
<a name="line533"></a>  assertTrue(&#39;Selection should exist&#39;, goog.dom.Range.hasSelection());
<a name="line534"></a>
<a name="line535"></a>  goog.dom.Range.clearSelection();
<a name="line536"></a>
<a name="line537"></a>  assertFalse(&#39;Selection should not exist&#39;, goog.dom.Range.hasSelection());
<a name="line538"></a>}
<a name="line539"></a>
<a name="line540"></a>function assertForward(string, startNode, startOffset, endNode, endOffset) {
<a name="line541"></a>  var root = goog.dom.getElement(&#39;test2&#39;);
<a name="line542"></a>  var originalInnerHtml = root.innerHTML;
<a name="line543"></a>
<a name="line544"></a>  assertFalse(string, goog.dom.Range.isReversed(startNode, startOffset,
<a name="line545"></a>      endNode, endOffset));
<a name="line546"></a>  assertTrue(string, goog.dom.Range.isReversed(endNode, endOffset,
<a name="line547"></a>      startNode, startOffset));
<a name="line548"></a>  assertEquals(&#39;Contents should be unaffected after: &#39; + string,
<a name="line549"></a>      root.innerHTML, originalInnerHtml);
<a name="line550"></a>}
<a name="line551"></a>
<a name="line552"></a>function testIsReversed() {
<a name="line553"></a>  var root = goog.dom.getElement(&#39;test2&#39;);
<a name="line554"></a>  var text1 = root.firstChild; // Text content: &#39;abc&#39;.
<a name="line555"></a>  var br = root.childNodes[1];
<a name="line556"></a>  var text2 = root.lastChild; // Text content: &#39;def&#39;.
<a name="line557"></a>
<a name="line558"></a>  assertFalse(&#39;Same element position gives false&#39;, goog.dom.Range.isReversed(
<a name="line559"></a>      root, 0, root, 0));
<a name="line560"></a>  assertFalse(&#39;Same text position gives false&#39;, goog.dom.Range.isReversed(
<a name="line561"></a>      text1, 0, text2, 0));
<a name="line562"></a>  assertForward(&#39;Element offsets should compare against each other&#39;,
<a name="line563"></a>      root, 0, root, 2);
<a name="line564"></a>  assertForward(&#39;Text node offsets should compare against each other&#39;,
<a name="line565"></a>      text1, 0, text2, 2);
<a name="line566"></a>  assertForward(&#39;Text nodes should compare correctly&#39;,
<a name="line567"></a>      text1, 0, text2, 0);
<a name="line568"></a>  assertForward(&#39;Text nodes should compare to later elements&#39;,
<a name="line569"></a>      text1, 0, br, 0);
<a name="line570"></a>  assertForward(&#39;Text nodes should compare to earlier elements&#39;,
<a name="line571"></a>      br, 0, text2, 0);
<a name="line572"></a>  assertForward(&#39;Parent is before element child&#39;, root, 0, br, 0);
<a name="line573"></a>  assertForward(&#39;Parent is before text child&#39;, root, 0, text1, 0);
<a name="line574"></a>  assertFalse(&#39;Equivalent position gives false&#39;, goog.dom.Range.isReversed(
<a name="line575"></a>      root, 0, text1, 0));
<a name="line576"></a>  assertFalse(&#39;Equivalent position gives false&#39;, goog.dom.Range.isReversed(
<a name="line577"></a>      root, 1, br, 0));
<a name="line578"></a>  assertForward(&#39;End of element is after children&#39;, text1, 0, root, 3);
<a name="line579"></a>  assertForward(&#39;End of element is after children&#39;, br, 0, root, 3);
<a name="line580"></a>  assertForward(&#39;End of element is after children&#39;, text2, 0, root, 3);
<a name="line581"></a>  assertForward(&#39;End of element is after end of last child&#39;,
<a name="line582"></a>      text2, 3, root, 3);
<a name="line583"></a>}
<a name="line584"></a>
<a name="line585"></a>function testSelectAroundSpaces() {
<a name="line586"></a>  // set the selection
<a name="line587"></a>  var textNode = goog.dom.getElement(&#39;textWithSpaces&#39;).firstChild;
<a name="line588"></a>  goog.dom.TextRange.createFromNodes(
<a name="line589"></a>      textNode, 5, textNode, 12).select();
<a name="line590"></a>
<a name="line591"></a>  // get the selection and check that it matches what we set it to
<a name="line592"></a>  var range = goog.dom.Range.createFromWindow();
<a name="line593"></a>  assertEquals(&#39; world &#39;, range.getText());
<a name="line594"></a>  assertEquals(5, range.getStartOffset());
<a name="line595"></a>  assertEquals(12, range.getEndOffset());
<a name="line596"></a>  assertEquals(textNode, range.getContainer());
<a name="line597"></a>
<a name="line598"></a>  // Check the contents again, because there used to be a bug where
<a name="line599"></a>  // it changed after calling getContainer().
<a name="line600"></a>  assertEquals(&#39; world &#39;, range.getText());
<a name="line601"></a>}
<a name="line602"></a>
<a name="line603"></a>function testSelectInsideSpaces() {
<a name="line604"></a>  // set the selection
<a name="line605"></a>  var textNode = goog.dom.getElement(&#39;textWithSpaces&#39;).firstChild;
<a name="line606"></a>  goog.dom.TextRange.createFromNodes(
<a name="line607"></a>      textNode, 6, textNode, 11).select();
<a name="line608"></a>
<a name="line609"></a>  // get the selection and check that it matches what we set it to
<a name="line610"></a>  var range = goog.dom.Range.createFromWindow();
<a name="line611"></a>  assertEquals(&#39;world&#39;, range.getText());
<a name="line612"></a>  assertEquals(6, range.getStartOffset());
<a name="line613"></a>  assertEquals(11, range.getEndOffset());
<a name="line614"></a>  assertEquals(textNode, range.getContainer());
<a name="line615"></a>
<a name="line616"></a>  // Check the contents again, because there used to be a bug where
<a name="line617"></a>  // it changed after calling getContainer().
<a name="line618"></a>  assertEquals(&#39;world&#39;, range.getText());
<a name="line619"></a>}
<a name="line620"></a>
<a name="line621"></a>function testRangeBeforeBreak() {
<a name="line622"></a>  var container = goog.dom.getElement(&#39;rangeAroundBreaks&#39;);
<a name="line623"></a>  var text = container.firstChild;
<a name="line624"></a>  var offset = text.length;
<a name="line625"></a>  assertEquals(4, offset);
<a name="line626"></a>
<a name="line627"></a>  var br = container.childNodes[1];
<a name="line628"></a>  var caret = goog.dom.Range.createCaret(text, offset);
<a name="line629"></a>  caret.select();
<a name="line630"></a>  assertEquals(offset, caret.getStartOffset());
<a name="line631"></a>
<a name="line632"></a>  var range = goog.dom.Range.createFromWindow();
<a name="line633"></a>  assertFalse(&#39;Should not contain whole &lt;br&gt;&#39;,
<a name="line634"></a>      range.containsNode(br, false));
<a name="line635"></a>  if (goog.userAgent.IE) {
<a name="line636"></a>    assertTrue(&#39;Range over &lt;br&gt; is adjacent to the immediate range before it&#39;,
<a name="line637"></a>        range.containsNode(br, true));
<a name="line638"></a>  } else {
<a name="line639"></a>    assertFalse(&#39;Should not contain partial &lt;br&gt;&#39;,
<a name="line640"></a>        range.containsNode(br, true));
<a name="line641"></a>  }
<a name="line642"></a>
<a name="line643"></a>  assertEquals(offset, range.getStartOffset());
<a name="line644"></a>  assertEquals(text, range.getStartNode());
<a name="line645"></a>}
<a name="line646"></a>
<a name="line647"></a>function testRangeAfterBreak() {
<a name="line648"></a>  var container = goog.dom.getElement(&#39;rangeAroundBreaks&#39;);
<a name="line649"></a>  var br = container.childNodes[1];
<a name="line650"></a>  var caret = goog.dom.Range.createCaret(container.lastChild, 0);
<a name="line651"></a>  caret.select();
<a name="line652"></a>  assertEquals(0, caret.getStartOffset());
<a name="line653"></a>
<a name="line654"></a>  var range = goog.dom.Range.createFromWindow();
<a name="line655"></a>  assertFalse(&#39;Should not contain whole &lt;br&gt;&#39;,
<a name="line656"></a>      range.containsNode(br, false));
<a name="line657"></a>  var isSafari3 =
<a name="line658"></a>      goog.userAgent.WEBKIT &amp;&amp; !goog.userAgent.isVersionOrHigher(&#39;528&#39;);
<a name="line659"></a>
<a name="line660"></a>  if (goog.userAgent.IE || isSafari3) {
<a name="line661"></a>    assertTrue(&#39;Range over &lt;br&gt; is adjacent to the immediate range after it&#39;,
<a name="line662"></a>        range.containsNode(br, true));
<a name="line663"></a>  } else {
<a name="line664"></a>    assertFalse(&#39;Should not contain partial &lt;br&gt;&#39;,
<a name="line665"></a>        range.containsNode(br, true));
<a name="line666"></a>  }
<a name="line667"></a>
<a name="line668"></a>  if (isSafari3) {
<a name="line669"></a>    assertEquals(2, range.getStartOffset());
<a name="line670"></a>    assertEquals(container, range.getStartNode());
<a name="line671"></a>  } else {
<a name="line672"></a>    assertEquals(0, range.getStartOffset());
<a name="line673"></a>    assertEquals(container.lastChild, range.getStartNode());
<a name="line674"></a>  }
<a name="line675"></a>}
<a name="line676"></a>
<a name="line677"></a>function testRangeAtBreakAtStart() {
<a name="line678"></a>  var container = goog.dom.getElement(&#39;breaksAroundNode&#39;);
<a name="line679"></a>  var br = container.firstChild;
<a name="line680"></a>  var caret = goog.dom.Range.createCaret(container.firstChild, 0);
<a name="line681"></a>  caret.select();
<a name="line682"></a>  assertEquals(0, caret.getStartOffset());
<a name="line683"></a>
<a name="line684"></a>  var range = goog.dom.Range.createFromWindow();
<a name="line685"></a>  assertTrue(&#39;Range over &lt;br&gt; is adjacent to the immediate range before it&#39;,
<a name="line686"></a>      range.containsNode(br, true));
<a name="line687"></a>  assertFalse(&#39;Should not contain whole &lt;br&gt;&#39;,
<a name="line688"></a>      range.containsNode(br, false));
<a name="line689"></a>
<a name="line690"></a>  assertRangeEquals(container, 0, container, 0, range);
<a name="line691"></a>}
<a name="line692"></a>
<a name="line693"></a>function testFocusedElementDisappears() {
<a name="line694"></a>  // This reproduces a failure case specific to Gecko, where an element is
<a name="line695"></a>  // created, contentEditable is set, is focused, and removed.  After that
<a name="line696"></a>  // happens, calling selection.collapse fails.
<a name="line697"></a>  // https://bugzilla.mozilla.org/show_bug.cgi?id=773137
<a name="line698"></a>  var disappearingElement = goog.dom.createDom(&#39;div&#39;);
<a name="line699"></a>  document.body.appendChild(disappearingElement);
<a name="line700"></a>  disappearingElement.contentEditable = true;
<a name="line701"></a>  disappearingElement.focus();
<a name="line702"></a>  document.body.removeChild(disappearingElement);
<a name="line703"></a>  var container = goog.dom.getElement(&#39;empty&#39;);
<a name="line704"></a>  var caret = goog.dom.Range.createCaret(container, 0);
<a name="line705"></a>  // This should not throw.
<a name="line706"></a>  caret.select();
<a name="line707"></a>  assertEquals(0, caret.getStartOffset());
<a name="line708"></a>}
<a name="line709"></a>
<a name="line710"></a>function assertNodeEquals(expected, actual) {
<a name="line711"></a>  assertEquals(
<a name="line712"></a>      &#39;Expected: &#39; + goog.testing.dom.exposeNode(expected) +
<a name="line713"></a>      &#39;\nActual: &#39; + goog.testing.dom.exposeNode(actual),
<a name="line714"></a>      expected, actual);
<a name="line715"></a>}
</pre>


</body>
</html>
